package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.business.dao.sys.SysAttachMapper;
import com.cloud.business.entity.sys.SysAttach;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysattachvm.SysAttachQueryModel;
import com.cloud.business.service.sys.ISysAttachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 系统附件表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2020-03-25
 */
@Service
public class SysAttachServiceImpl extends ServiceImpl<SysAttachMapper, SysAttach> implements ISysAttachService {

    private final SysAttachMapper sysAttachMapper;

    @Autowired
    public SysAttachServiceImpl(SysAttachMapper sysAttachMapper) {
        this.sysAttachMapper = sysAttachMapper;
    }

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysAttachQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysAttachQueryModel sysAttachQueryModel, IPage<SysAttach> page, ResultMsg msg) throws Exception {
        QueryWrapper<SysAttach> sysAttachQueryWrapper = new QueryWrapper<>();
        sysAttachQueryWrapper.lambda()
                .eq(!StringUtils.isEmpty(sysAttachQueryModel.getBusinessTable()), SysAttach::getBusinessTable, sysAttachQueryModel.getBusinessTable())
                .eq(sysAttachQueryModel.getBusinessId() != null && sysAttachQueryModel.getBusinessId() != 0,
                        SysAttach::getBusinessId, sysAttachQueryModel.getBusinessId())
                .like(!StringUtils.isEmpty(sysAttachQueryModel.getQueryStr()), SysAttach::getName, sysAttachQueryModel.getQueryStr())
                ;
        sysAttachMapper.selectPage(page, sysAttachQueryWrapper);
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param sysAttach
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysAttach sysAttach, ResultMsg msg) throws Exception {
        Date now=new Date();
        sysAttach.setCreateTime(now);
        sysAttach.setModificationTime(now);
        int count = sysAttachMapper.insert(sysAttach);
        msg.setMsg("创建成功");
        return sysAttach.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param sysAttach
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysAttach sysAttach, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysAttach.setModificationTime(now);
        int count = sysAttachMapper.updateById(sysAttach);
        msg.setMsg("更新成功");
        return sysAttach.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysAttach getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysAttach sysAttach = sysAttachMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysAttach;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysAttachMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

}
